var vue = new Vue({
    el : '#app',
    data : {
        typeTable:[],
        typeQueryForm:{
            current : 1,
            size : 10,
            total : 0,
        },
        typeForm:{
            typeId:0,
            dicCode:'',
            dicName:'',
            remark:'',
        },
        typeFormRules: {
            typeCode: [{required: true, message: '请输入类型编码'},],
            typeName: [{required: true, message: '请输入类型名称'},]
        },

        dicTable:[],
        dicQueryForm:{
            typeId:0,
        },
        dicForm : {
            typeId:0,
            dicKey:'',
            dicValue:'',
            dicOrder:0,
            remark:'',
        },
        dicFormRules: {
            dicKey: [{required: true, message: '请输入Key'},],
            dicValue: [{required: true, message: '请输入Value'},]
        },
        selectType:{},
        loading: false,
        typeFormVisible : false,
        dicformvisible : false,
    },
    created : function(){
        this.loadAllType();
    },
    methods : {
        saveType:function(){
            var self = this;

            this.$refs['typeForm'].validate(function (valid) {
                if (!valid) return false;

                $.ajax({
                    url: constant.ROOT_SITE + '/system/dictype/save',
                    data: JSON.stringify(self.typeForm),
                    success: function (result) {
                        if (result.code === 200) {
                            self.$message({
                                type: 'success',
                                message: '保存成功！'
                            });
                            self.typeFormVisible = false;
                            self.loadAllType();
                        } else {
                            self.$message.error(result.errMsg);
                        }
                    }
                })
            });
        },
        saveDic:function(){
            var self = this;
            this.dicForm.typeId = this.selectType.id;

            this.$refs['dicform'].validate(function (valid) {
                if (!valid) return false;
                $.ajax({
                    url: constant.ROOT_SITE + '/system/dic/save',
                    data: JSON.stringify(self.dicForm),
                    success: function (result) {
                        if (result.code === 200) {
                            self.$message({
                                type: 'success',
                                message: '保存成功！'
                            });
                            self.dicformvisible = false;
                            self.listDicByTypeId();
                        } else {
                            self.$message.error(result.errMsg);
                        }
                    }
                })
            });
        },
        loadAllType:function(){
            var self = this;
            $.ajax({
                url:  constant.ROOT_SITE + '/system/dictype/list' ,
                data: JSON.stringify(self.typeQueryForm),
                success: function (result) {
                    if (result.code === 200) {
                        self.typeTable = result.body;
                        self.typeQueryForm.total = result.total;
                    }
                }
            })
        },
        onTypeClick:function(dicType, column, event){
            this.selectType = dicType;
            this.listDicByTypeId();
        },

        listDicByTypeId:function(){
            var self = this;
            $.ajax({
                url:  constant.ROOT_SITE + '/system/dic/list/' + self.selectType.id,
                success: function (result) {
                    if (result.code === 200) {
                        self.dicTable = result.body;
                    }
                }
            })
        },
        deleteDic:function(dic){
            var self = this;

            this.$confirm('您确定用删除该字典值吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                $.ajax({
                    url:  constant.ROOT_SITE + '/system/dic/delete/' + dic.id,
                    success: function (result) {
                        if (result.code === 200) {
                            self.listDicByTypeId();
                        }
                    }
                })
            });
        },
        showTypeEditDialog:function(){
            this.typeFormVisible = true;
        },
        showDicEditDialog:function(dic){
            $.extend(this.dicForm,dic);
            this.dicformvisible = true;
        },
        onDicFormDialogClosed:function(){
            this.dicForm={
                typeId:0,
                dicKey:'',
                dicValue:'',
                dicOrder:0,
                remark:'',
            }
        },
        onTypeFormDialogClosed:function(){
            this.typeForm = {
                typeId:0,
                dicCode:'',
                dicName:'',
                remark:'',
            }
        },
        onTypeListSizeChange:function (pageSize) {
            this.typeForm.size = pageSize;
            this.loadAllType();
        },
        onTypeListCurrentChange:function (pageCurrent) {
            this.typeForm.current = pageCurrent;
            this.loadAllType();
        },
    }
});